﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class PublisherController
    {
        public IList<Publisher> GetAllPublisher()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Publisher>().ToList();
            }
        }

        public IList<String> GetAllPublisherName()
        {
            return GetAllPublisher().Select(publisher => publisher.Name).ToList();
        }

        public string GetPublisherName(Publisher publisher)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Publisher>().ToList().
                    Where(p => p.Id == publisher.Id).
                    Select(p => p.Name).
                    Single();
            }
        }

        public int AddPublisher(Publisher publisher)
        {
            try
            {
                GetAllPublisher()
                    .Where(p => p.Name == publisher.Name)
                    .Select(p => p)
                    .Single();

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.Save(publisher);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public int EditPublisher(Publisher newPublisher, String oldName)
        {
            try
            {
                var checkPublisher = GetAllPublisher()
                    .Where(p => p.Name == newPublisher.Name)
                    .Select(p => p)
                    .Single();

                if (checkPublisher.Name == oldName)
                {
                    using (var session = NHibernateHelper.OpenSession())
                    {
                        using (var transaction = session.BeginTransaction())
                        {
                            session.SaveOrUpdate(newPublisher);
                            transaction.Commit();
                        }
                    }

                    return 1;
                }

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(newPublisher);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public void DeletePublisher(Publisher publisher)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(publisher);
                    transaction.Commit();
                }
            }
        }
    }
}
